﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.ModuleData;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;

namespace Plumber.Sales.Forms
{
    public partial class LoadInvoices : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        private DRMasterTypeByType dRMasterTypeByType;
        private DRMasters dRMaster;
        private DRMasterType dRMasterType;
        private DRSalesInvoices dRSalesInvoices;
        private string adminType;
        private string condomType;

        public ListView.ListViewItemCollection List { get { return lsvInvoices.Items; } }

        public string MasterType { get { return cbbType.GetValue<string>(); } }

        public List<string> ListDocumentNo = new List<string>();

        public LoadInvoices()
        {
            InitializeComponent();
            InitializeAuxComponent();
        }

        private void InitializeAuxComponent()
        {
            dRMaster = new DRMasters(false);
            dRMasterType = new DRMasterType(DRMasterType.Module.All);
            dRMasterTypeByType = new DRMasterTypeByType();
            dRSalesInvoices = new DRSalesInvoices(false);
            if (dRMasterTypeByType.Find(MasterTypes.E_Condominium))
                condomType = dRMasterTypeByType.GetValue<string>(PL_MasterType.Code, 0);

            if (dRMasterTypeByType.Find(MasterTypes.E_Administrator))
            {
                adminType = dRMasterTypeByType.GetValue<string>(PL_MasterType.Code, 0);
                rdrAdmin.AttachCodeType(adminType);
                rdrAdmin.RadarForm = new RadarMasterForm(adminType, RadarMasterForm.Module.Master);
                rdrAdmin.RadarForm.EnableAddOnFly = false;
                rdrAdmin.DescriptionControl = lblDescAdmin;
            }

            rdrMaster.RadarForm = new RadarMasterForm(RadarMasterForm.Module.Master);
            rdrMaster.RadarForm.EnableAddOnFly = false;
            rdrMaster.DescriptionControl = lblDescMaster;

            cbbType.AttachDataReader(new RRMasterTypeForInvoice());
            cbbType_SelectedValueChanged(this, EventArgs.Empty);
        }

        private void cbbType_SelectedValueChanged(object sender, EventArgs e)
        {
            rdrAdmin.Enabled = cbbType.GetValue<string>() == condomType;
            if (!rdrAdmin.Enabled)
                rdrAdmin.Text = string.Empty;

            dRMasterType.Find(cbbType.GetValue<string>());
            rdrMaster.Enabled = dRMasterType.GetValue<CodeMode>(PL_MasterType.CodeMode) != CodeMode.E_IsEmpty;
            if (!rdrMaster.Enabled)
                rdrMaster.ResetText();
            else
            {
                rdrMaster.AttachCodeType(cbbType.GetValue<string>());
                rdrMaster.RadarForm.FilterType = cbbType.GetValue<string>();
            }
            lblDescMaster.Text = string.Empty;
            lblDescAdmin.Text = string.Empty;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            lsvInvoices.Items.Clear();

            QueryBuilder qb = new QueryBuilder();
            qb.SetQualified = true;
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelectAll(PL_SalesInvoices.Name);
                qb.AddFrom(PL_SalesInvoices.Name);

                qb.AddCompare<int>(PL_SalesInvoices.InvoiceType, InvoiceType.E_InvoiceEm.Int());

                qb.AddCompare<string>(PL_SalesInvoices.Type, cbbType.GetValue<string>());

                if (!rdrMaster.IsEmpty)
                {
                    SqlABParameter p1 = new SqlABParameter("@p1", PL_SalesInvoices.Master);
                    qb.AddCompare(PL_SalesInvoices.Master, p1);
                    scc.Parameters.Add(p1);
                    p1.Value = rdrMaster.Text;
                }

                if (cbbType.GetValue<string>() == condomType && !String.IsNullOrEmpty(rdrAdmin.Text))
                {
                    SqlABParameter p2 = new SqlABParameter("@p2", PL_Masters.Administrator);
                    qb.AddCompare(PL_SalesInvoices.Administrator, p2);
                    scc.Parameters.Add(p2);
                    p2.Value = rdrAdmin.Text;
                }

                qb.AddOrder(PL_SalesInvoices.ReleaseDate);
                qb.AddOrder(PL_SalesInvoices.InvoiceNo);

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                try
                {
                    while (dr.Read())
                    {
                        AddRow(dr);
                    }
                    dr.Close();

                    foreach (ColumnHeader col in lsvInvoices.Columns)
                        col.Width = -1;
                }
                catch (Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                }
            }
        }

        private void AddRow(SqlABDataReader dr)
        {
            ListViewItem lvi = new ListViewItem(dr.GetValue<string>(PL_SalesInvoices.InvoiceNo));
            lvi.SubItems.Add(dr.GetFormatValue<DateTime>(PL_SalesInvoices.ReleaseDate));
            lvi.SubItems.Add(dr.GetFormatValue<string>(PL_SalesInvoices.Master));

            if (dRMaster.Find(dr.GetValue<string>(PL_SalesInvoices.Type), dr.GetValue<string>(PL_SalesInvoices.Master)))
                lvi.SubItems.Add(dRMaster.GetValue<string>(PL_Masters.Description));
            else
                lvi.SubItems.Add("???");

            lsvInvoices.Items.Add(lvi);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lsvInvoices_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (dRSalesInvoices.Find(global::InvoiceType.E_InvoiceEm, e.Item.SubItems[0].Text))
                rtbBody.Rtf = dRSalesInvoices.GetValue<string>(PL_SalesInvoices.InvoiceBody);
        }
    }
}